package com.example.kfcclient;

import java.util.ArrayList;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;

public class OrderListAdapter extends ArrayAdapter<OrderItem> implements OnTouchListener  {

	private final ArrayList<OrderItem> values;
	private final Activity context;
	public OrderListAdapter(Activity context,ArrayList<OrderItem> values) {
		super(context, R.layout.order_row,values);
		this.values = values;
		this.context = context;
		// TODO Auto-generated constructor stub
	}
	static class ViewHolder
	{
		protected TextView name;
		protected EditText number;
		protected TextView price;
	}
	/* (non-Javadoc)
	 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = null;
		if(convertView == null)
		{
			LayoutInflater inflater = context.getLayoutInflater();
			view = inflater.inflate(R.layout.order_row, null);
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.name = (TextView) view.findViewById(R.id.order_name);
			viewHolder.price = (TextView) view.findViewById(R.id.order_price);
			viewHolder.number = (EditText) view.findViewById(R.id.order_number);
			viewHolder.number.setOnTouchListener(this);
			view.setOnTouchListener(this);
			viewHolder.number.addTextChangedListener(new TextWatcher() {
				public void onTextChanged(CharSequence s, int start, int before, int count) {
					// TODO Auto-generated method stub
					OrderItem item = (OrderItem) viewHolder.number.getTag();
					item.setNumber(s.toString());
				}
				
				public void beforeTextChanged(CharSequence s, int start, int count,
						int after) {
					// TODO Auto-generated method stub
					
				}
				
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					
				}
			});
			viewHolder.number.setTag(values.get(position));
			
			view.setTag(viewHolder);
			
		}
		else
		{
			view = convertView;
			((ViewHolder) view.getTag()).number.setTag(values.get(position));
		}
		
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.number.setText(values.get(position).getNumber());
		holder.name.setText(values.get(position).getName());
		holder.price.setText(values.get(position).getPrice()+" vnđ");
		return view;
	}
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if(v instanceof EditText)
		{
			EditText editText = (EditText) v;
			editText.setFocusable(true);
			editText.setFocusableInTouchMode(true);
		}
		else
		{
			ViewHolder holder = (ViewHolder) v.getTag();
			holder.number.setFocusable(false);
			holder.number.setFocusableInTouchMode(false);
		}
		return false;
	}
	
	public String getItemName(int position)
	{
		OrderItem orderItem = (OrderItem) values.get(position);
		return orderItem.getName();
		
	}
	public int getItemPrice(int position)
	{
		OrderItem orderitem = (OrderItem) values.get(position);
		return orderitem.getPrice();
	}
	public String getItemNumber(int position)
	{
		OrderItem orderitem = (OrderItem) values.get(position);
		return orderitem.getNumber();
	}
	/* (non-Javadoc)
	 * @see android.widget.ArrayAdapter#getCount()
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return values.size();
	}

}
